package org.pim.smart.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.pim.smart.hibernate.domain.SysUser;
import org.pim.smart.hibernate.domain.SysUserDAO;
import org.pim.smart.utils.PasswordEncrpt;
import org.pim.smart.utils.SystemContext;

public class LoginServiceImpl implements LoginService {

	private SysUserDAO sysUserDao;
	
	public LoginServiceImpl(){
		sysUserDao = new SysUserDAO();
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean checkUserIdAndPwd(String userId, String userPwd) {
		// TODO Auto-generated method stub
		List<SysUser> userList = sysUserDao.findByUserId(userId);
		if (userList == null || userList.size() == 0){
			return false;
		} else {
			SysUser tempUser = (SysUser)userList.get(0);
			if (tempUser.getUserPwd().equals(userPwd)){
				tempUser.setLastLogin(new Date());
				Transaction tx = this.sysUserDao.getSession().beginTransaction();
				try {
					sysUserDao.attachDirty(tempUser);
					tx.commit();
				} catch (HibernateException e) {
					// TODO Auto-generated catch block
					tx.rollback();
					e.printStackTrace();
				}
				SystemContext context = SystemContext.getInstance();
				context.setUser(tempUser);
				return true;
			} else {
				return false;
			}
		}
	}

	@Override
	public void releaseDataBaseResource() {
		// TODO Auto-generated method stub
		if (this.sysUserDao != null){
			sysUserDao.getSession().close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> getRecentLoginUser() {
		// TODO Auto-generated method stub
		Transaction tx = null;
		List<String> recentLogin = new ArrayList<String>();
		List<SysUser> result = null;
		try{
			Session session = this.sysUserDao.getSession();
			tx = session.beginTransaction();
			Query query = session.createQuery("from " + SysUser.class.getName()+ " as user order by user.lastLogin desc");
			query.setMaxResults(5);
			result = query.list();
			if (result != null && result.size() > 0){
				for (Iterator<SysUser> it = result.iterator(); it.hasNext(); ){
					SysUser tempUser = it.next();
					recentLogin.add(tempUser.getUserId());
				}
			}
		} catch (HibernateException e){
			if (tx != null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return recentLogin;
	}

	@SuppressWarnings("unchecked")
	@Override
	public SysUser getUserByUserId(String userId) {
		// TODO Auto-generated method stub
		List<SysUser> result = this.sysUserDao.findByUserId(userId);
		if (result != null && result.size() == 1){
			return result.get(0);
		} else {
			return null;
		}
	}

	@Override
	public void updateUserPassword(SysUser user, String newPassword) {
		// TODO Auto-generated method stub
		user.setUserPwd(PasswordEncrpt.encrptPwd(newPassword));
		Transaction tx = this.sysUserDao.getSession().beginTransaction();
		try {
			sysUserDao.attachDirty(user);
			tx.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			tx.rollback();
			e.printStackTrace();
		}
	}
}
